import transformDate from "./transformDate";

/**
 * 获取最近时间（前后）的时间
 * @param count
 * @param start_time
 * @param type
 * @returns {string}
 */
export default function timeForMat (count, start_time, type = 'D') {
    let dd = transformDate(start_time);
    if (dd === false) dd = new Date();
    switch (type) {
        case "Y":
            dd.setFullYear(dd.getFullYear() + count);//前后time_ab年
            break;
        case "M":
            dd.setMonth(dd.getMonth() + count);//前后time_ab月
            break;
        case "D":
            dd.setDate(dd.getDate() + count);//前后time_ab天
            break;
        case "h":
            dd.setHours(dd.getHours() + count);//前后time_ab小时
            break;
        case "m":
            dd.setMinutes(dd.getMinutes() + count);//前后time_ab分
            break;
        case "s":
            dd.setSeconds(dd.getSeconds() + count);//前后time_ab秒
            break;
    }
    let y = dd.getFullYear();
    let m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);//获取月份，不足10补0
    let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();//获取日期，不足10补0
    let hh = dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours();//获取小时，不足10补0
    let mm = dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes();//获取分，不足10补0
    let ss = dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds();//获取秒，不足10补0
    return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
}
